package com.example.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.project.service.ShopService;
import com.example.project.mapper.ShopMapper;
import com.example.project.mapper.UserMapper;
import com.example.project.entity.Shop;
import com.example.project.entity.User;
import com.example.project.common.Result;
import com.example.project.dto.request.ShopRequest;
import com.example.project.dto.response.ShopResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class ShopServiceImpl implements ShopService {

    private final ShopMapper shopMapper;
    private final UserMapper userMapper;

    @Override
    public Result<ShopResponse> getShopInfo(Long userId) {
        // 检查用户是否是商家
        User user = userMapper.selectById(userId);
        if (user == null || user.getRoleId() != 3) {
            return Result.error("非商家用户");
        }

        // 获取店铺信息
        Shop shop = shopMapper.selectOne(
            new LambdaQueryWrapper<Shop>()
                .eq(Shop::getUserId, userId)
        );
        if (shop == null) {
            return Result.error("店铺不存在");
        }

        ShopResponse response = new ShopResponse();
        BeanUtils.copyProperties(shop, response);
        return Result.success(response);
    }

    @Override
    @Transactional
    public Result<?> createShop(ShopRequest request, Long userId) {
        // 检查用户是否是商家
        User user = userMapper.selectById(userId);
        if (user == null || user.getRoleId() != 3) {
            return Result.error("非商家用户");
        }

        // 检查是否已有店铺
        Shop existShop = shopMapper.selectOne(
            new LambdaQueryWrapper<Shop>()
                .eq(Shop::getUserId, userId)
        );
        if (existShop != null) {
            return Result.error("您已经有店铺了");
        }

        // 检查店铺名称是否重复
        Shop nameExist = shopMapper.selectOne(
            new LambdaQueryWrapper<Shop>()
                .eq(Shop::getShopName, request.getShopName())
        );
        if (nameExist != null) {
            return Result.error("店铺名称已存在");
        }

        // 创建店铺
        Shop shop = new Shop();
        BeanUtils.copyProperties(request, shop);
        shop.setUserId(userId);
        shop.setStatus(1);
        shopMapper.insert(shop);

        return Result.success();
    }

    @Override
    @Transactional
    public Result<?> updateShop(ShopRequest request, Long userId) {
        // 获取店铺信息
        Shop shop = shopMapper.selectOne(
            new LambdaQueryWrapper<Shop>()
                .eq(Shop::getUserId, userId)
        );
        if (shop == null) {
            return Result.error("店铺不存在");
        }

        // 检查店铺名称是否重复
        if (!shop.getShopName().equals(request.getShopName())) {
            Shop nameExist = shopMapper.selectOne(
                new LambdaQueryWrapper<Shop>()
                    .eq(Shop::getShopName, request.getShopName())
            );
            if (nameExist != null) {
                return Result.error("店铺名称已存在");
            }
        }

        // 更新店铺信息
        BeanUtils.copyProperties(request, shop);
        shopMapper.updateById(shop);

        return Result.success();
    }

    @Override
    @Transactional
    public Result<?> updateShopStatus(Long userId, Integer status) {
        // 获取店铺信息
        Shop shop = shopMapper.selectOne(
            new LambdaQueryWrapper<Shop>()
                .eq(Shop::getUserId, userId)
        );
        if (shop == null) {
            return Result.error("店铺不存在");
        }

        // 更新店铺状态
        shop.setStatus(status);
        shopMapper.updateById(shop);

        return Result.success();
    }

    @Override
    public Result<ShopResponse> getShopDetail(Long shopId) {
        // 获取店铺信息
        Shop shop = shopMapper.selectById(shopId);
        if (shop == null || shop.getStatus() != 1) {
            return Result.error("店铺不存在或已关闭");
        }

        ShopResponse response = new ShopResponse();
        BeanUtils.copyProperties(shop, response);
        return Result.success(response);
    }
} 